<?php


namespace app\controller;


use app\model\dictionaryModel;
use think\facade\Request;

class dictionary
{
    private $model;

    public function __construct()
    {
        $this->model = new dictionaryModel();
    }

    public function getList()
    {
        $param=Request::get();
        $map = [];
        if (isset($param['type_id'])&&$param['type_id']){
            $map[] = ['type_id', '=', $param['type_id']];
        }
        $data=$this->model->with(['type'])->where($map)->paginate([
            'query' => $param,
            'var_page' => 'current_page',
            'list_rows' => isset($param['pagesize'])?$param['pagesize']:10,
        ])->toArray();
        file_put_contents('1.txt', var_export($param, 1));
        $dictionarys = dictionaryModel::has('categorys', '>', 0)->select()->toArray();
        foreach ($data['data'] as &$dictionary) {
            if (in_array($dictionary['id'], array_column($dictionarys,'id'))) {
                $dictionary['hasChildren'] = true;
            }
        }
        if ($data) $meta=['status'=>200];
        else $meta=['status'=>400];
        return json(['data'=>$data,'meta'=>$meta]);
    }
    public function add()
    {
        $data = Request::post();
        $this->model->addData($data);
        return json(['meta'=>['msg'=>'新增成功','status'=>200]]);
    }
    public function delete($id)
    {
        // 删除
        $this->model->deleteData($id);
        return json(['meta'=>['msg'=>'删除成功','status'=>200]]);
    }
}